#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
using namespace std;

struct ListNode {
     int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
};
//打印链表
void displayList(ListNode *p){
    while( p ){
        cout<< p->val ;
        p = p->next;
        if(p) cout<<"->";
    }
    cout<<endl;
}

//解决方案类
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *p1 = new ListNode(0,head),*p2=p1,*mhead=p1;
        for(int i =0;i<n;i++) p2 = p2->next;
        while(p2->next) p2 = p2->next , p1 = p1->next;
        p1->next = p1->next->next;
        return mhead->next;
    }
};

int main(){
    Solution sp;
    ListNode *l1 = new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5)))));
    ListNode *p = sp.removeNthFromEnd(l1,2);
    displayList(p);
    return 0;
}